package com.suguowen.service;

import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import  com.suguowen.model.GMCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Service;

@Service
public class GMService {
    @Autowired
    DefaultConversionService defaultConversionService;
    private static String reflectLocation = "com.suguowen.model.GMCommand";

    /**
     * 反射并执行指令
     * order:args：methodName arg1 arg2...
     * @Author suguowen
     * @date 2019/2/17 16:47
     */
    public void reflect(String order){
        try {
            // 校验参数不为空,null
            if (order != null && "".equals(order)){
                throw new IllegalArgumentException("参数'" + order + "'异常");
            }
            // 反射类
            Class gmCommand = Class.forName(reflectLocation);
            // split：方法名 + 参数
            Pattern p = Pattern.compile("\\s+");
            Matcher m = p.matcher(order);
            String replaceStr= m.replaceAll(" ");
            String[] split = replaceStr.split(" ");

            // 反射GMCommand
            invokeGMCommand(gmCommand, split);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 参数工厂:根据参数的个数，选择调用的方法（参数上限：3)
     * @Author suguowen
     * @date 2019/2/2 22:23
     */
    public void invokeGMCommand(Class  clazz, String[] split) throws Exception {
            GMCommand gmCommand = new GMCommand();
            int length = split.length;
            String param1;
            String param2;
            String param3;
            Class<?> class1;
            Class<?> class2;
            Class<?> class3;

            // 找gm方法
            String methodName = split[0];
            Method[] methods = clazz.getDeclaredMethods();
            Method targetMethod = null;
            for(Method method : methods ){
                if (methodName.equals(method.getName())){
                    targetMethod = method;
                }
            }
            // 参数工厂
            switch (length) {
                case 1:
                    targetMethod.invoke(gmCommand);
                    break;
                case 2:
                    param1 = split[1];
                    class1 = targetMethod.getParameterTypes()[0];
                    targetMethod.invoke(gmCommand,defaultConversionService.convert(param1,class1));
                    break;
                case 3:
                    param1 = split[1];
                    param2 = split[2];
                    class1 = targetMethod.getParameterTypes()[0];
                    class2 = targetMethod.getParameterTypes()[1];
                    targetMethod.invoke(gmCommand,defaultConversionService.convert(param1,class1),defaultConversionService.convert(param2,class2));
                    break;
                case 4:
                    param1 = split[1];
                    param2 = split[2];
                    param3 = split[3];
                    class1 = targetMethod.getParameterTypes()[0];
                    class2 = targetMethod.getParameterTypes()[1];
                    class3 = targetMethod.getParameterTypes()[2];
                    targetMethod.invoke(gmCommand,defaultConversionService.convert(param1,class1),
                            defaultConversionService.convert(param2,class2),
                            defaultConversionService.convert(param3,class3));
                    break;
            }
    }
}
